﻿using System;
using System.ComponentModel;
using System.Globalization;
using TomanuExtensions;

namespace Raytracer.PropertyGridDecorators
{
    public class SingleConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext a_context,
            Type a_source_type)
        {
            if (a_source_type == typeof(string))
                return true;

            return base.CanConvertFrom(a_context, a_source_type);
        }

        public override bool CanConvertTo(ITypeDescriptorContext a_context,
            Type a_destination_type)
        {
            if (a_destination_type == typeof(float))
                return true;

            return base.CanConvertTo(a_context, a_destination_type);
        }

        public override object ConvertFrom(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value)
        {
            if (a_value is string)
                return (a_value as string).ToSingle();

            return base.ConvertFrom(a_context, a_culture, a_value);
        }

        public override object ConvertTo(ITypeDescriptorContext a_context,
            CultureInfo a_culture, object a_value,
            Type a_destinationType)
        {
            if (a_destinationType == typeof(string))
                return ((float)a_value).ToString(CultureInfo.InvariantCulture);

            return base.ConvertTo(a_context, a_culture, a_value, a_destinationType);
        }
    }
}